package com.wy.service;

import com.github.pagehelper.util.StringUtil;
import com.wy.bean.Dianpu;
import com.wy.bean.Ds;
import com.wy.bean.Shop;
import com.wy.dao.DianpuDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DianpuServiceImpl implements DianpuService{

    @Resource
    private DianpuDao dianpuDao;

    @Override
    public List<Dianpu> list(Dianpu dianpu) {
        return dianpuDao.list(dianpu);
    }

    @Override
    public void insert(Dianpu dianpu) {
        dianpuDao.insert(dianpu);
        bth(dianpu);
    }

    private void bth(Dianpu dianpu) {
        String sids = dianpu.getSids();
        if(StringUtil.isNotEmpty(sids)){
            ArrayList list = new ArrayList();
            String[] sid = sids.split(",");
            for (String s : sid) {
                Ds ds = new Ds();
                ds.setDid(dianpu.getDid());
                ds.setSid(s);
                list.add(ds);
            }
            dianpuDao.insertbth(list);
        }
    }

    @Override
    public List<Shop> getShops() {
        return dianpuDao.getShops();
    }

    @Override
    public Dianpu getById(String did) {
        return dianpuDao.getById(did);
    }

    @Override
    public void update(Dianpu dianpu) {
        //修改店铺表
        dianpuDao.update(dianpu);
        //删除中间表旧的关联数据
        dianpuDao.deleteBydid(dianpu.getDid());
        //添加中间表数据
        bth(dianpu);
    }

    @Override
    public void deletebth(String[] dids) {
        dianpuDao.deletebth(dids);
        dianpuDao.deleteDsbth(dids);
    }
}
